package com.elock.entity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;

import com.elock.base.core.entity.BaseEntity;

/**
 * Entity - 机构
 * 
 * @author 仁德
 */
@Entity
@Table(name = "sale_org")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "sale_org_sequence")
public class SaleOrg extends BaseEntity {

	private static final long serialVersionUID = 6719859631792431453L;

	/** 树路径分隔符 */
	public static final String ID_PATH_SEPARATOR = ",";

	/** 是否启用 */
	private Boolean isEnabled;

	/** 名称 */
	private String name;

	/** 机构编号 */
	private String sn;

	/** 上一级 */
	private SaleOrg parent;

	/** 层级 */
	private Integer grade;

	/** Id路径 */
	private String treePath;

	/** 机构类型 0.公司 1.客户 2.油库 */
	private Long type;

	/** 是否为顶级组织 */
	private Boolean isTop;

	/** 唯一标识 UUID */
	private String uniqueIdentify;

	/** 是否叶子端 */
	private Boolean isLeaf;

	/** 备注 */
	private String memo;

	/** 子级 */
	private Set<SaleOrg> children = new HashSet<SaleOrg>();

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	/**
	 * 获取 isEnabled
	 * 
	 * @return isEnabled
	 */
	public Boolean getIsEnabled() {
		return isEnabled;
	}

	/**
	 * 设置 isEnabled
	 * 
	 * @param isEnabled
	 */
	public void setIsEnabled(Boolean isEnabled) {
		this.isEnabled = isEnabled;
	}

	/**
	 * 获取 parent
	 * 
	 * @return parent
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	public SaleOrg getParent() {
		return parent;
	}

	/**
	 * 设置 parent
	 * 
	 * @param parent
	 */
	public void setParent(SaleOrg parent) {
		this.parent = parent;
	}

	/**
	 * 获取 children
	 * 
	 * @return children
	 */
	@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
	public Set<SaleOrg> getChildren() {
		return children;
	}

	/**
	 * 设置 children
	 * 
	 * @param children
	 */
	public void setChildren(Set<SaleOrg> children) {
		this.children = children;
	}

	/**
	 * 获取 name
	 * 
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * 设置 name
	 * 
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	@Column(unique = true)
	public String getSn() {
		return sn;
	}

	public void setSn(String sn) {
		this.sn = sn;
	}

	/**
	 * 获取 grade
	 * 
	 * @return the grade
	 */
	public Integer getGrade() {
		return grade;
	}

	/**
	 * 设置 grade
	 * 
	 * @param grade
	 *            the grade to set
	 */
	public void setGrade(Integer grade) {
		this.grade = grade;
	}

	/**
	 * 获取 treePath
	 * 
	 * @return the treePath
	 */
	public String getTreePath() {
		return treePath;
	}

	/**
	 * 设置 treePath
	 * 
	 * @param treePath
	 *            the treePath to set
	 */
	public void setTreePath(String treePath) {
		this.treePath = treePath;
	}

	/**
	 * 获取 type
	 * 
	 * @return the type
	 */
	public Long getType() {
		return type;
	}

	/**
	 * 设置 type
	 * 
	 * @param type
	 *            the type to set
	 */
	public void setType(Long type) {
		this.type = type;
	}

	/**
	 * 持久化前处理
	 */
	@PrePersist
	public void prePersist() {
		SaleOrg parent = getParent();
		if (parent != null) {
			setTreePath(parent.getTreePath() + parent.getId() + ID_PATH_SEPARATOR);
		} else {
			setTreePath(ID_PATH_SEPARATOR);
		}
		setGrade(getIdPaths().size());
		if (getUniqueIdentify() == null) {
			setUniqueIdentify(UUID.randomUUID().toString());
		}
	}

	/**
	 * 更新前处理
	 */
	@PreUpdate
	public void preUpdate() {
		SaleOrg parent = getParent();
		if (parent != null) {
			setTreePath(parent.getTreePath() + parent.getId() + ID_PATH_SEPARATOR);
		} else {
			setTreePath(ID_PATH_SEPARATOR);
		}
		setGrade(getIdPaths().size());
	}

	/**
	 * 获取树路径
	 * 
	 * @return 树路径
	 */
	@Transient
	public List<Long> getIdPaths() {
		List<Long> treePaths = new ArrayList<Long>();
		String[] ids = StringUtils.split(getTreePath(), ID_PATH_SEPARATOR);
		if (ids != null) {
			for (String id : ids) {
				treePaths.add(Long.valueOf(id));
			}
		}
		return treePaths;
	}

	/**
	 * 获取是否为顶级组织
	 * 
	 * @date 2016年10月18日 下午4:47:24
	 * @return
	 */
	public Boolean getIsTop() {
		return isTop;
	}

	/**
	 * 设置是否为顶级组织
	 * 
	 * @date 2016年10月18日 下午4:47:21
	 * @param isTop
	 */
	public void setIsTop(Boolean isTop) {
		this.isTop = isTop;
	}

	/**
	 * 获取 唯一标识 UUID
	 * 
	 * @date 2017年8月18日
	 * @return uniqueIdentify
	 */
	@Column(updatable = false)
	public String getUniqueIdentify() {
		return uniqueIdentify;
	}

	/**
	 * 设置 唯一标识 UUID
	 * 
	 * @date 2017年8月18日
	 * @param uniqueIdentify
	 *            唯一标识 UUID
	 */
	public void setUniqueIdentify(String uniqueIdentify) {
		this.uniqueIdentify = uniqueIdentify;
	}

	/**
	 * 获取 是否叶子端
	 * 
	 * @return isLeaf
	 */
	public Boolean getIsLeaf() {
		return isLeaf;
	}

	/**
	 * 设置 是否叶子端
	 * 
	 * @param isLeaf
	 *            是否叶子端
	 */
	public void setIsLeaf(Boolean isLeaf) {
		this.isLeaf = isLeaf;
	}
}